package com.roamer.javase.demo.message.service;

import com.roamer.javase.demo.message.dao.MessageMapper;
import com.roamer.javase.demo.message.dto.MessagePageQueryDTO;
import com.roamer.javase.demo.message.entity.MessageEntity;
import com.roamer.javase.demo.message.vo.MessageBaseVO;
import com.roamer.javase.demo.message.vo.MessageForDayVO;
import com.roamer.javase.demo.message.vo.PageResult;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author roamer
 * @version v1.0
 * @date 2020/3/17 22:36
 */
public class MessageService {
    public static final String DELIMITER = ",";

    private MessageMapper messageMapper;

    public PageResult<MessageForDayVO> pageList(MessagePageQueryDTO pageQueryDTO) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        // 按照时间排序，升序
        List<MessageBaseVO> messageList = messageMapper.getByQueryDTO(pageQueryDTO);
        Map<String, MessageForDayVO> messageForDayMap = new LinkedHashMap<>();
        for (MessageBaseVO message : messageList) {
            // 区域ID,广场ID,设备ID,时间(yyyyMMdd)
            String key = message.getQyId().
                    concat(DELIMITER).
                    concat(message.getGcId())
                    .concat(DELIMITER)
                    .concat(message.getDeviceId().toString())
                    .concat(DELIMITER)
                    .concat(sdf.format(message.getCreateDate()));
            MessageForDayVO messageForDay = messageForDayMap.get(key);
            if (null == messageForDay) {
                messageForDay = new MessageForDayVO();
                // 省略部分set方法
                messageForDay.setDeviceId(message.getDeviceId());
                // 放入map
                messageForDayMap.put(key, messageForDay);
            }
            messageForDay.addCount();
        }
        List<MessageForDayVO> list = new ArrayList<>(messageForDayMap.values());
        // 处理阀值
        for (Iterator<MessageForDayVO> it = list.iterator(); it.hasNext(); ) {
            MessageForDayVO vo = it.next();
            if (vo.getCount() < pageQueryDTO.getThreshold()) {
                it.remove();
            }
        }
        // 处理分页数据
        PageResult<MessageForDayVO> result = new PageResult<>();
        int startRow = (pageQueryDTO.getPageNo() - 1) * pageQueryDTO.getPageSize();
        int endRow = pageQueryDTO.getPageNo() * pageQueryDTO.getPageSize();
        if (startRow >= list.size()) {
            result.setData(new ArrayList<>());
        } else {
            result.setData(list.subList(startRow, Math.min(endRow, list.size())));
        }
        result.setTotalRows(list.size());
        result.setTotalPages(list.size() % pageQueryDTO.getPageSize() == 0 ? list.size() % pageQueryDTO.getPageSize() : list.size() % pageQueryDTO.getPageSize() + 1);
        return result;
    }

    public List<Long> getDeviceIdList() throws InterruptedException {
        Thread.sleep(10000);
        return Arrays.asList(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L);
    }

    public List<MessageEntity> getDeviceInfo(Long id) throws InterruptedException {
        Thread.sleep(12000);
        List<MessageEntity>  list = new ArrayList<>();
        for (int i = 1; i < 25; i++) {
            MessageEntity messageEntity = new MessageEntity();
            // 获取小时
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, i);
            messageEntity.setCreateDate(calendar.getTime());
            messageEntity.setGcId("" + i);
            messageEntity.setDeviceId(id);
            list.add(messageEntity);
        }
        return list;
    }
}
